package com.timothy.android;

import java.util.List;
import com.timothy.service.MessageSenderUtil;
import com.timothy.statistics.LoginBuilder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class HomeDemoActivity extends BaseProcessActivity {
	private static final String LOG_TAG = "HomeDemoActivity";
	
//	private String[] demoSessions = {"Science","Life","History","English","Other"};
	private String[] demoSessions;
	private String selectedSession;
	
	private TextView homeTitle = null;
	
	private Spinner spinner;
	private ArrayAdapter<String> adapter;
//	private Spinner spinner2;
//	private ArrayAdapter<String> adapter2;
	
	private Button enterBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.i(LOG_TAG,"onCreate()...");
		
		setContentView(R.layout.home_demo);
		
		homeTitle = (TextView) this.findViewById(R.id.homeTitle);
		homeTitle.setText("Demo Vote");
		
		spinner = (Spinner) findViewById(R.id.Spinner01);
//		spinner2 = (Spinner) findViewById(R.id.Spinner02);
		
		//get demoSessionStr and save to string array
		Bundle bundle = this.getIntent().getExtras();
		String demoSessionStr = bundle.getString("demoSessionStr");
		List<String> dSessionList = LoginBuilder.parseDemoSession(demoSessionStr);
		demoSessions = dSessionList.toArray(new String[dSessionList.size()]); 
		
		adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,demoSessions);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
		spinner.setVisibility(View.VISIBLE);
		
		enterBtn = (Button) findViewById(R.id.enterBtn);
		enterBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
//				Intent intent = new Intent();
//				intent.setClass( getBaseContext(), LayoutA.class);
//				startActivity(intent);
				
				Log.i(LOG_TAG,"selectedSession:"+selectedSession);
				
				if(selectedSession!=null&&selectedSession.length()>0){
					sendMessage(MessageSenderUtil.sendDemoSessionSelection(selectedSession));
				}else{
					ShowDialog("Please choose one session!");
				}

			}			
		});
		
	}
	
	
/*	public void setSpinner2(String[] qArray){
		adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,qArray);
		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner2.setAdapter(adapter2);
		spinner2.setOnItemSelectedListener(new SpinnerSelectedListener2());
		spinner2.setVisibility(View.VISIBLE);		
	}*/
	
	//使用数组形式操作
	class SpinnerSelectedListener implements OnItemSelectedListener{
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
//			String[] qArray = null;
//			
//			switch (arg2){
//				case 0: qArray = new String[]{"Science1","Science2","Science3","Science4","Science5"};break;
//				case 1: qArray = new String[]{"Life1","Life2","Life3","Life4","Life5"};break;
//				case 2: qArray = new String[]{"History1","History2","History3","History4","History5"};break;
//				case 3: qArray = new String[]{"English1","English2","English3","English4","English5"};break;
//				case 4: qArray = new String[]{"Other1","Other2","Other3","Other4","Other5"};break;
//			}
//			
//			setSpinner2(qArray);
			
			selectedSession = demoSessions[arg2];

		}
		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}
	
/*	class SpinnerSelectedListener2 implements OnItemSelectedListener{
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
		}
		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}*/

	@Override
	protected void processMessage(int what, String content) {
		super.processMessage(what, content);
		
		Log.i(LOG_TAG,"processMessage()...");
		Log.i(LOG_TAG,"type:"+ String.valueOf(what));
		Log.i(LOG_TAG,"content:"+ content);
		
		/*if (CommunicationService.MSG_CONNECT_SESSION != what){
			
		}
		*/
	}
}